<template>
  <div>
    <div class="list-table">
      <div class="area-header">
        <span class="area-header-title">{{ $t('银行账户') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item v-if="!businessId" :label="$t('主体类型')" prop="subjectType">
          <dictSelect
            v-model="queryParams.subjectType"
            :clearable="true"
            dictionary="main_body_type_all"
          />
        </el-form-item>
        <el-form-item v-if="!businessId" :label="$t('主体名称')" prop="subjectId">
          <MainBodySelect
            v-model="queryParams.subjectId"
            :parameters="queryParams.subjectType"
          />
        </el-form-item>
        <el-form-item :label="$t('账户类型')" prop="accountType">
          <el-select v-model="queryParams.accountType" :placeholder="$t('账户类型')" clearable>
            <el-option
              v-for="dict in accountTypeOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('账户名称')" prop="accountName">
          <el-input
            v-model="queryParams.accountName"
            :placeholder="$t('账户名称')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('开户行')" prop="openingBank">
          <el-input
            v-model="queryParams.openingBank"
            :placeholder="$t('开户行')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['fund:account:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['fund:account:export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="exportHandle"
          >{{ $t("导出") }}</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="fundAccountList" border>
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column v-if="!businessId" :label="$t('主体类型')" align="center" prop="subjectType" :formatter="subjectTypeFormat" />
        <el-table-column v-if="!businessId" :label="$t('主体名称')" align="center" prop="subjectName" show-overflow-tooltip />
        <el-table-column :label="$t('账户类型')" align="center" prop="accountType" :formatter="accountTypeFormat" />
        <el-table-column :label="$t('账户名称')" align="center" prop="accountName">
          <template slot-scope="scope">
            <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.accountName }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('开户行')" align="center" prop="openingBank" />
        <el-table-column :label="$t('账号')" align="center" prop="accountNumber" />
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['fund:account:edit']"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}</el-button>
            <el-button
              v-hasPermi="['fund:account:remove']"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改账户信息对话框 -->
      <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
        <fundAccountForm ref="fundAccountForm" />
      </el-dialog>
    </div>
    <!-- 证券账户 -->
    <fund-security-list style="margin-top: 20px;" />
  </div>
</template>

<script>
import { listFundAccount, delFundAccount } from '@/api/fund/account/fundAccount'
import fundAccountForm from './fundAccountForm.vue'
import { downLoadExcel } from '@/utils/excelDownload'
import FundSecurityList from '@/views/fund/security/fundSecurityList'
import MainBodySelect from '@/components/MainBodySelect/index'
export default {
    components: {
      FundSecurityList,
      fundAccountForm,
      MainBodySelect
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 账户信息表格数据
          fundAccountList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
        // 账户类型字典
        accountTypeOptions: [],
        subjectTypeOptions: [],
          // 查询参数
          queryParams: {
              fundId: this.$route.query.id,
              subjectType: undefined,
              subjectId: undefined,
              accountType: undefined,
              accountName: undefined,
              openingBank: undefined,
              accountNumber: undefined,
              pageNum: 1,
              pageSize: 10
          },
          businessId: this.$route.query.id,
          // 表单参数
          form: {},
          // 表单校验
          rules: {
        accountType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], accountName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] }
      }
  },
  created() {
      this.getList()
      this.getDicts('fund_account_type').then(response => {
          this.accountTypeOptions = response.data
      })
    this.getDicts('main_body_type_all').then(response => {
      this.subjectTypeOptions = response.data
    })
  },
  methods: {
    /** 查询账户信息列表 */
    getList() {
        this.loading = true
        listFundAccount(this.queryParams).then(response => {
            this.fundAccountList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    subjectTypeFormat(row) {
      return this.selectDictLabel(this.subjectTypeOptions, row.subjectType)
    },
    // 账户类型字典翻译
    accountTypeFormat(row) {
      return this.selectDictLabel(this.accountTypeOptions, row.accountType)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            subjectType: undefined,
            subjectId: undefined,
            accountType: undefined,
            accountName: undefined,
            openingBank: undefined,
            accountNumber: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('添加账户信息')
        this.$nextTick(() => {
            this.$refs.fundAccountForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('修改账户信息')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.fundAccountForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delFundAccount(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        downLoadExcel('/account/fundAccount/export', this.queryParams)
    }
  }
}
</script>
